﻿/**
 * 
 */
package action;
import dao.*;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author Administrator
 *
 */
public class RegisterAction extends ActionSupport{
	private String username;
	private String password;
	private String repassword;
	private String email;
	private String nickname;
	private String school;
	private String tip;
	
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUsername() {
		return username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return password;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getRepassword() {
		return repassword;
	}	
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail() {
		return email;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getNickname() {
		return nickname;
	}
	public void setSchool(String school) {
		this.school = school;
	}
	public String getSchool() {
		return school;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public String getTip() {
		return tip;
	}
	
	@Override
	public String execute() throws Exception{
		ActionContext ac=ActionContext.getContext();
		Map session=ac.getSession();		
		
		HttpServletRequest request=(HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);
		String ip=request.getRemoteAddr();
		UsersDAO dao=new UsersDAO();
		Session hsession=HibernateSessionFactory.getSession();
		Users u=new Users(username, password, nickname, school,ip);
		if((username.length()==0||password.length()==0||nickname.length()==0||school.length()==0)){
			//ac.put("tip", "用户名、密码、昵称、学院不能为空");
			addActionError("用户名、密码、昵称、学院不能为空");
			return INPUT;
		}
		hsession.beginTransaction();
		dao.save(u);
	    hsession.getTransaction().commit();
	    //注册成功后登录
	    ActionContext.getContext().getSession().put("User",username);
    	tip="欢迎您，"+username;
		return SUCCESS;
	}

}
